var log = document.getElementsByClassName('log')[0]
var reg = document.getElementsByClassName('reg')[0]
var lgc = getCookie('lgc')
if (lgc) {
    log.innerHTML = lgc
    reg.innerHTML = '退出登录'
    reg.onclick = function () {
        delCookie('lgc')
        log.innerHTML = `<a href="./login.html">请登录</a>`
        reg.innerHTML = `<a href="./register.html">注册有礼</a>`
    }
}



var showNum = document.getElementsByClassName("showNum")[0]
var searchSite = document.getElementsByClassName('search-site')[0]
var pageNext = document.getElementsByClassName('next')[0]
var pagePrev = document.getElementsByClassName('prev')[0]
var tips = document.getElementsByClassName("tips")[0]
var goodInfo = urlParamsParse().goodInfo ? urlParamsParse().goodInfo : ''
var searchBtn = document.getElementsByClassName('btn')[0]
var tips = document.getElementsByClassName('tips')[0]
var goodList = document.getElementsByClassName('goods-list')[0]
console.log(goodInfo)
var seartips = document.getElementsByClassName('seartips')[0]
if (goodInfo) {
    seartips.innerHTML = `我们为您提供 '${goodInfo}' 的搜索结果`
} else {
    seartips.innerHTML = `您可以尝试搜索 '苹果'  '小米'  '一加'`
}
var col = "id"
var type = "asc"
var page = 1
var size = 10
var maxPage = null
goodsLoad()

searchBtn.onclick = function () {
    goodInfo = this.previousElementSibling.value
    if (goodInfo) {
        seartips.innerHTML = `我们为您提供 '${goodInfo}' 的搜索结果`
    } else {
        seartips.innerHTML = `您可以尝试搜索 '苹果'  '小米'  '一加'`
    }

    goodsLoad()
}


searchSite.addEventListener('click', clickSearch)
function clickSearch(e) {
    var e = e || window.event
    var target = e.target
    if (target.className == 'orderCol') {
        col = target.value
        goodsLoad()
    } else if (target.className == 'orderType') {
        type = target.value
        goodsLoad()
    } else if (target.className == "reset") {
        goodInfo = ""
        col = "id"
        type = "asc"
        page = 1
        size = 10
        goodsLoad()
    }
}
showNum.onchange = function () {
    size = this.value
    goodsLoad()
}
pageNext.onclick = function () {
    page++
    goodsLoad()
}
pagePrev.onclick = function () {
    if (page <= 1) {
        alert("已经是第一页了")
        return false
    }
    page--
    goodsLoad()
}
function goodsLoad() {
    searchGoodsOrderLimit({ wd: goodInfo, col, type, page, size }).then(res => {
        var { maxPage, current, list, message } = res
        var html = ``
        list.forEach((item) => {
            var { goodsId, goodsName, goodsPrice, goodsImg } = item
            html += ` <li>
                <a href="./details.html?goodInfo=${goodsId}">
                    <img src="${goodsImg}" alt="">
                        <div class="price">￥${goodsPrice}</div>
                        <div class="desc">${goodsName}</div>
                </a>
            </li>`
        })
        if (message != 'OK') {
            html = '暂无商品哦,请重新输入关键字哦'
        }
        goodList.innerHTML = html
        if (page > maxPage) {
            alert('已经是最后一页啦')
        }

        page = page > maxPage ? maxPage : page
        tips.textContent = `${current}/${maxPage}`
        if (current == undefined || maxPage == undefined) {
            tips.textContent = `${0}/${0}`
        }
    })
}